package com.example.jdbc0914.dao;

import org.apache.commons.dbcp2.BasicDataSource;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author ：lqs
 * @date ：Created in 2023/9/14 14:43
 * @description：
 * @modified By：
 * @version:
 */
public class DBUtil {
    private static String DRIVER_CLASS;
    private static String DB_URL;
    private static String DB_USER;
    private static String DB_PASS;

    private static int DB_MINIdLE;
    private static int DB_MAXIdLE;


    /**
     * @description: 加载配置、注册驱动
     * @create time 2023/8/30 15:04
     * @param null
     * @return
     */
    static {
        //  创建 Properties 对象
        Properties properties = new Properties();
        //  读取 Properties 属性文件到输入流中
        InputStream resourceAsStream = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        //  从输入流中加载属性列表
        try {
            properties.load(resourceAsStream);
            DRIVER_CLASS = properties.getProperty("driver");
            DB_URL = properties.getProperty("url");
            DB_USER = properties.getProperty("user");
            DB_PASS = properties.getProperty("password");
            DB_MINIdLE = Integer.valueOf(properties.getProperty("minIdle"));
            DB_MAXIdLE = Integer.valueOf(properties.getProperty("maxIdle"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //  加载驱动
    static {
        try {
            Class.forName(DRIVER_CLASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    /**
     * @return java.sql.Connection
     * @description: 获取连接
     * @create time 2023/8/30 15:05
     */
    static Connection getConnection() {
        //  创建连接池
        BasicDataSource dataSource = new BasicDataSource();
        Connection connection = null;
        try {
            dataSource.setUrl(DB_URL);
            dataSource.setUsername(DB_USER);
            dataSource.setPassword(DB_PASS);
            dataSource.setMinIdle(DB_MINIdLE);
            dataSource.setMaxIdle(DB_MAXIdLE);
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }


    static void closeConnection(Connection connection, Statement statement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}


